import pandas as pd
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.globals import ThemeType
from pyecharts.render import make_snapshot
from snapshot_pyppeteer import snapshot
from tqdm import tqdm


def main(datasource, d_province_name):
    reader = pd.ExcelFile(datasource)
    dfs = []
    for sheet in reader.sheet_names:
        df = reader.parse(sheet_name=sheet)
        dfs.append(df)
    concat_dfs = pd.concat(dfs, axis=0)
    d_prov_df = concat_dfs[concat_dfs.d_province == d_province_name]
    d_prov_df = d_prov_df.drop(labels='Unnamed: 0', axis=1)
    x_data = list(d_prov_df)
    x_data.remove('o_province')
    x_data.remove('d_province')
    y_columns = list(d_prov_df)
    y_columns.remove('d_province')
    y_datas = d_prov_df[y_columns].values.tolist()
    line = (
        Line()
        .set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=True, position='right'),
            legend_opts=opts.LegendOpts(pos_left="left"),
            xaxis_opts=opts.AxisOpts(type_="category"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            title_opts=opts.TitleOpts(
                title=d_province_name+"流入图", pos_left="right")
        )
        .add_xaxis(xaxis_data=x_data)
    )
    for item in y_datas:
        line.add_yaxis(
            series_name=item[0],
            y_axis=item[1:],
            symbol="emptyCircle",
            is_symbol_show=True,
            is_smooth=True,
            label_opts=opts.LabelOpts(is_show=False),
        )
    line.render(d_province_name+".html")


if __name__ == '__main__':
    city = pd.read_json(r'F:\baidu_insight_big_data\CityCode.json')
    NameList = list(city['省名称'].unique())
    with tqdm(total=len(NameList)) as pbar:
        for i in NameList:
            main(datasource=r'F:\baidu_insight_big_data\2021-01-01to2021-03-01provincetoprovince.xlsx',
                 d_province_name=i)
            pbar.update(1)
    # main(datasource=r'F:\baidu_insight_big_data\2021-01-01to2021-03-01provincetoprovince.xlsx',
    #      d_province_name='浙江省')
